-module(mnesia_index_test).
-compile(export_all).

-record(person, {name, age}).

start() ->
    mnesia:create_schema([node()]),
    mnesia:start(),
    mnesia:create_table(person, [
                                    {disc_copies, [node()]},
                                    {type, set},
                                    {index, [age]}, % add index on age
                                    {local_content, true},
                                    {attributes, record_info(fields,person)}
                                ]),
        
    mnesia:stop().

reset() ->
    mnesia:start(),
    mnesia:wait_for_tables([person], 10000),
    mnesia:clear_table(person),
    P = #person{name=hh, age=37},
    F = fun() ->
            mnesia:write(P)
        end,
    mnesia:transaction(F).

test() ->
    F = fun() ->
            P1 = mnesia:read({person, hh}),
            P2 = mnesia:index_read(person, 37, 3), %{person, Name, Age}
            {P1, P2}
        end,
    mnesia:transaction(F).
